package ServiceInterface;

import java.sql.SQLException;
import javax.jws.WebService;
import Beans.*;
import Validation.Validate;
import Database.DatabaseInterface;

@WebService
public class Service {
	private DatabaseInterface db = new DatabaseInterface();
	

	public String signIn(String email, String password) {
		System.out.println("(Server->Service): Inside Sign-in");
		String result = null;

		result = db.signIn(email, password);
		
		return result;
	}
	
	public Integer createAccount(Member member_info) throws SQLException {
		
		System.out.println("Inside CreateAccount");
		
//		if (checkMember(member_info))
			return db.createAccount(member_info);
//		else
//			return 0;
	}

	public boolean deleteExistingMember(String email) throws SQLException {
		System.out.println("Inside deleteExistingMember");
		
		return db.deleteExistingMember(email);
	}
	
	
	public Integer createMovie(Movie movie) throws SQLException {
		System.out.println("Inside createMovie");
		
//		if (checkMovie(movie))
			return db.createMovie(movie);
//		else
//			return 0;
	}
	
	public boolean deleteMovie(Integer movie_id) {
		System.out.println("Inside deleteMovie");
		
		return db.deleteMovie(movie_id);
	}
	
	public boolean rentMovie(String email, Integer movie_id) {
		System.out.println("Inside rentMovie");
		
		return db.rentMovie(email, movie_id);
	}
	
	public boolean returnMovie(String email, Integer movie_id) {
		System.out.println("Inside returnMovie");
		
		return db.returnMovie(email, movie_id);
	}
	
	public Member[] listPremiumMembers() {
		System.out.println("Inside listPremiumMembers");
		return db.listPremiumMembers();
	}
	
	public Member[] listSimpleMembers() {
		System.out.println("Inside listSimpleMembers");
		return db.listSimpleMembers();
	} 
	
	public Movie[] listMovies() {
		System.out.println("Inside listMovies");
		return db.listMovies();
	}
	
	public Member[] listAllMembers() {
		System.out.println("Inside listAllMembers");
		return db.listAllMembers();
	}
	
	public Integer updateMemberInformation(Member member_info) {

		System.out.println("Inside updateMemberInformation");
		
		return 0;
	}
	
	public Integer updateMovieInformation(Movie movie_info) {

		System.out.println("Inside updateMemberInformation");
		
		return 0;
	}
	
	public Member[] searchMember(Member member_info) {
		
		System.out.println("Inside searchMember");
		
		return db.searchMember(member_info);
	}
	
	public Movie[] searchMovie(Movie movie_info) {
		
		System.out.println("Inside searchMovie");
		
		return db.searchMovie(movie_info);

	}
	
	public Member displayMember(Integer user_id) {
		
		System.out.println("Inside displayMember");
		
		return new Member();	
	}
	
	public String getMembershipType(String email) {
		System.out.println("Inside generateBill");
		String membership_type = null;
		String delim = "[$]";
		
		//		if(Validate.validateEmail(email) == false) {
		//			System.out.println("Invalid email address!");
		//			return null; //TO DO: this should be more meaningful
		//		}
		
		String membership_result = db.getMembershipType(email);
		String[]tokens = membership_result.split(delim);
		if(tokens[0].equals("true")) {
			membership_type = tokens[1];
		} else {
			return tokens[1]; // error message
		}
		
		return membership_type;
	}
	
	public Movie[] generateBill(String email) {
		System.out.println("Inside generateBill");
		
		//		if(Validate.validateEmail(email) == false) {
		//			System.out.println("Invalid email address!");
		//			return null; //TO DO: this should be more meaningful
		//		}
		
		//		String membership_type = null;
		//		String delim = "[$]";
		
		//Get user membership type
		//		String membership_result = db.getMembershipType(email);
		//		String[]tokens = membership_result.split(delim);
		//		if(tokens[0].equals("true")) {
		//			membership_type = tokens[1];
		//		} else {
		//			return tokens[1]; // error message
		//		}
		
		//Get movies rented by user "BORROW"
		return db.getMovieIdsBorrowedByUser(email);
	}
	
	//This helper function checks if the movie has valid information
	private boolean checkMovie(Movie movie) {
		if (movie.getTitle().length() > 128 || movie.getTitle().length() == 0) return false;
		if (movie.getBanner().length() > 128 || movie.getBanner().length() == 0) return false;
		
		String date = movie.getDate();
		
		if (date == null) return false;
		else {
			String[] dataArray = date.split("/");
			if (dataArray.length != 3) return false;
			
			for (String num: dataArray) 
				if (!num.matches("\\d+")) return false;
		}
		
		if (movie.getRentalFee() <= 0) return false;
		
		if (movie.getCategory().length() > 32 || movie.getCategory().length() == 0) return false;
		
		if (movie.getQuantity() <= 0) return false;
		
		return true;
		
	}
	
	private boolean checkMember(Member member) {
		System.out.println("In CheckMember");
		if (member.getEmail().length() > 64 || member.getEmail().length() == 0) return false;
		if (member.getFirst().length() > 32 || member.getFirst().length() == 0) return false;
		if (member.getLast().length() > 32 || member.getLast().length() == 0) return false;
		if (member.getAddress().length() > 256 || member.getAddress().length() == 0) return false;
		if (member.getCity().length() > 32 || member.getCity().length() == 0) return false;
		if (member.getState().length() > 32 || member.getState().length() == 0) return false;
		if (member.getZip() > 99999 || member.getState().length() < 0) return false;
		if (member.getPassword().length() > 128 || member.getPassword().length() < 0) return false;

		return true;
		
	}
	
}
